package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.user.UserLoginDTO;
import com.sky.entity.Category;
import com.sky.entity.SetMeal;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.User.UserService;
import com.sky.utils.JwtUtils;
import com.sky.vo.UserDishVO;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 褚金龙
 * @Date 2024/9/16 23:05
 */
@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin("*")

/**
 * 微信小程序登录业务
 */
@Api(tags = "微信小程序登录")
public class UserController {
   @Autowired
   private UserService userService;
   @Autowired
   private JwtProperties jwtProperties;
   @Autowired
   private RedisTemplate redisTemplate;
    @ApiOperation("微信登录请求")
    @PostMapping("/user/login")
    public Result<UserLoginVO> getToken(@RequestBody UserLoginDTO userLoginDTO){
        log.info("userLoginDTO{}",userLoginDTO);
        User user = userService.wxLogin(userLoginDTO);
        //为微信用户生成jwt令牌
        Map<String, Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtils.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        System.out.println(userLoginVO );
        return  Result.success(userLoginVO);
    }


       @ApiOperation("根据类型查询分类表")
      @GetMapping("/category/list")

     public Result categoryList(Integer type){
           log.info("{}",type);
           List<Category> categories = userService.categoryList(type);
           return Result.success(categories);
     }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/dish/list")
    @Cacheable(cacheNames = "dishCache" ,key = "#categoryId")//设置缓存
    public Result<List<UserDishVO>> dishList( Integer categoryId){
       //构造redis中的key,规则：dish_分类id
      /*  String key="dish_"+categoryId;

        //查询redis 中是否存在菜品数据
        List<UserDishVO> list = (List<UserDishVO>) redisTemplate.opsForValue().get(key);

        if (list != null && list.size()>0) {
            //如果存在，直接返回，无序查询数据库
         return Result.success(list);
        }
        // 如果不存在，查询数据库，将查询到的数据库放入redis中
            list = userService.dishList(categoryId);
        redisTemplate.opsForValue().set(key,list);*/
        List<UserDishVO> list = userService.dishList(categoryId);

        return Result.success(list);
    }


    @ApiOperation("根据分类id查询套餐")
    @GetMapping("/setmeal/list")
  @Cacheable(cacheNames = "setmealCache" ,key = "#categoryId")//添加对应的缓存数据
    public Result setMealList( Integer categoryId){
        log.info("categoryId{}",categoryId);
       List<SetMeal> setMeals= userService.setMealList(categoryId);
        return Result.success(setMeals);
    }




}
